package com.example.asus.mydiary;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.asus.db.domain.Note;
import com.example.asus.db.gen.NoteDao;
import com.example.asus.db.service.GreenDaoManager;

import java.util.List;

public class DiaryRecyclerAdapter extends RecyclerView.Adapter<DiaryRecyclerAdapter.mViewHolder>{
    private List<Note> noteData;  //存储数据列表
    private Note note;
    private NoteDao noteDao;

    /**
     * 传送数据
     * @param noteList
     */
    public DiaryRecyclerAdapter(List<Note> noteList){
        noteData = noteList;
    }

    /**
     *  初始化界面控件
     */
    public class mViewHolder extends RecyclerView.ViewHolder {
        private TextView tvtitle;
        private TextView tvbody;
        private TextView tvdate;
        private FloatingActionButton floatingActionButton;
        public mViewHolder(View itemView) {
            super(itemView);

            tvtitle = (TextView)itemView.findViewById(R.id.card_tvtitle);
            tvbody = (TextView)itemView.findViewById(R.id.card_tvbody);
            tvdate = (TextView)itemView.findViewById(R.id.card_tvdate);
            floatingActionButton = (FloatingActionButton)itemView.findViewById(R.id.delete_btn);
        }
    }

    /**
     *  创建页面布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public DiaryRecyclerAdapter.mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mViewHolder holder = new mViewHolder(LayoutInflater.
                from(parent.getContext()).inflate(R.layout.card_view,parent,false));

        return holder;
    }

    /**
     *  绑定数据
     * @param holder 设置对应的布局
     * @param position 该数据当前所在的位置
     */
    @Override
    public void onBindViewHolder(final DiaryRecyclerAdapter.mViewHolder holder, final int position) {
        /**
         *  遍历所有信息
         */
        String bodySize; // 用于简略信息

        // 检测字符串长度是否太长
        bodySize = noteData.get(holder.getAdapterPosition()).getBody();
        if(bodySize.length()>=35){
            bodySize=bodySize.substring(0,35);
            bodySize+="......";
            holder.tvbody.setText(bodySize);
        }
        else{
            holder.tvbody.setText(noteData.get(holder.getAdapterPosition()).getBody().toString());
        }

        holder.tvtitle.setText(noteData.get(holder.getAdapterPosition()).getTitle().toString());
        holder.tvdate.setText(noteData.get(holder.getAdapterPosition()).getDate().toString());

        /**
         *  自定义点击事件
         */
        if(mListener!=null){
            // 绑定点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(position);
                }
            });

            /**
             *  删除按钮
             */
            holder.floatingActionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onDeleteItemClick(position);
                }
            });
        }


    }

    /**
     *  返回数据数目
     * @return
     */
    @Override
    public int getItemCount() {
        return noteData == null?0:noteData.size();
    }

    /**
     * 定义一个接口
     */
    public interface ItemClickListener {
        void onItemClick(int position);
        void onDeleteItemClick(int position);
    }

    public ItemClickListener mListener;

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mListener = listener;
    }

//    //  删除数据
//    public void removeData(int position) {
//
//        noteData.remove(position);
//        //删除动画
//        notifyItemRemoved(position);
//        notifyDataSetChanged();
//    }
}
